/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.beans;

import com.logic.HostAdmin;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author root
 */
@ManagedBean
@ViewScoped
public class TerminalBean {

    private String command;
    private ArrayList<String> lstCmds;
    private String commandOut;
    private boolean terminalVisible;

    public TerminalBean() {
        command = "none";
        commandOut = "none";
        terminalVisible = false;
        lstCmds = new ArrayList<String>();
    }

    public void showTerminal() {
        terminalVisible = terminalVisible ? false : true;
    }

    public String sendCommand() {
        if (!command.equals("none")) {
            if (command.equals("clear")) {
                commandOut = "limpio";
            } else {
                if (command.startsWith("cd ")) {
                    lstCmds.add(command);
                    return commandOut = "dale otro comando ;)";
                } else {
                    lstCmds.add(command);
                    commandOut = HostAdmin.getInstance().executeCommand(lstCmds);
                }
            }
        }
        return "none";
    }

    public void execDaemonComand(ActionEvent e) {
//        HostAdmin.getInstance().setHostname(HostList.selectedHost);
//        int signal = Integer.parseInt(e.getComponent().getId().replace("a", ""));
//        Service service = (new ArrayList<Service>(getSelectedService().keySet())).get(0);
//        HostAdmin.getInstance().adminDaemon(signal, service);
    }

    public boolean getVisibleTerm() {
        return terminalVisible;
    }

    public boolean setVisibleTerm() {
        return terminalVisible;
    }

    public String getPrompt() {
        return "";//HostAdmin.getInstance().getPrompt();
    }

    public String getCommand() {
        return command.equals("none") ? "" : command;
    }

    public String getCommandOut() {
        return commandOut;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    public static void main(String[] args) {
        TerminalBean term = new TerminalBean();
        System.out.println("COM:" + term.command + "   OUT:" + term.commandOut + "======================");
        term.command = "ls /";
        System.out.println("COM:" + term.command + "   OUT:" + term.commandOut + "======================");
        term.sendCommand();
        System.out.println("COM:" + term.command + "   OUT:" + term.commandOut + "======================");
        term.command = "clear";
        term.sendCommand();
        System.out.println("COM:" + term.command + "   OUT:" + term.commandOut + "======================");
    }
//    public String getPrompt() {
//        prompt = executeCommand("\n");
//
//        if (prompt.length() > 0) {
//            int i = prompt.indexOf("[");
//            int j = prompt.indexOf("]");
//
//            prompt = prompt.substring(i, j + 2);
//        } else {
//            prompt = "Sin Conexión";
//        }
//      return "jajaja";
//    }
}
